#include "MenuClass.h"

using namespace frost;
using namespace menu;

MenuClass::MenuClass(void)
{
	m_D3D = 0;
	m_Camera = 0;
	m_TextureShader = 0;
	m_Header = 0;
	m_Start = 0;
	m_HighScoreList = 0;
	m_LoadGame = 0;
	m_SaveGame = 0;
	m_Quit = 0;
}

MenuClass::MenuClass(const MenuClass& other)
{

}

MenuClass::~MenuClass(void)
{
}

bool MenuClass::Initialize(int screenWidth, int screenHeight, HWND hwnd)
{
	bool result;
	D3DXMATRIX baseViewMatrix;

	
	// Create the Direct3D object.
	m_D3D = new D3DClass;
	if(!m_D3D)
	{
		return false;
	}

	// Initialize the Direct3D object.
	result = m_D3D->Initialize(screenWidth, screenHeight, menu::VSYNC_ENABLED, hwnd, menu::FULL_SCREEN, menu::SCREEN_DEPTH, menu::SCREEN_NEAR);
	if(!result)
	{
		MessageBox(hwnd, L"Could not initialize Direct3D.", L"Error", MB_OK);
		return false;
	}

	// Create the camera object.
	m_Camera = new CameraClass();
	if(!m_Camera)
	{
		return false;
	}

	m_Header = new TextClass();
	if(!m_Header)
	{
		return false;
	}

	m_Start = new TextClass();
	if(!m_Start)
	{
		return false;
	}

	m_HighScoreList = new TextClass();
	if(!m_HighScoreList)
	{
		return false;
	}

	m_Quit = new TextClass();
	if(!m_Quit)
	{
		return false;
	}

	// Set the initial position of the camera.
	//m_Camera->SetPosition(0.0f, 0.0f, -10.0f);
	m_Camera->SetPosition(0.0f, 0.0f, -1.0f);
	m_Camera->Render();
	m_Camera->GetViewMatrix(baseViewMatrix);

	// Create the texture shader object.
	m_TextureShader = new TextureShaderClass;
	if(!m_TextureShader)
	{
		return false;
	}

	result = m_Header->Initialize(m_D3D->GetDevice(), m_D3D->GetDeviceContext(), hwnd, "FairyFly Menu", screenWidth, screenHeight, 300, 50, baseViewMatrix);
	if(!result)
	{
		MessageBox(hwnd, L"Could not initialize the text object.", L"Error", MB_OK);
		return false;
	}

	result = m_Start->Initialize(m_D3D->GetDevice(), m_D3D->GetDeviceContext(), hwnd, "Start new game", screenWidth, screenHeight, 300, 70, baseViewMatrix);
	if(!result)
	{
		MessageBox(hwnd, L"Could not initialize the text object.", L"Error", MB_OK);
		return false;
	}

	result = m_HighScoreList->Initialize(m_D3D->GetDevice(), m_D3D->GetDeviceContext(), hwnd, "High Score", screenWidth, screenHeight, 300, 90, baseViewMatrix);
	if(!result)
	{
		MessageBox(hwnd, L"Could not initialize the text object.", L"Error", MB_OK);
		return false;
	}

	result = m_Quit->Initialize(m_D3D->GetDevice(), m_D3D->GetDeviceContext(), hwnd, "Avsluta", screenWidth, screenHeight, 300, 110, baseViewMatrix);
	if(!result)
	{
		MessageBox(hwnd, L"Could not initialize the text object.", L"Error", MB_OK);
		return false;
	}

	return true;
}

void MenuClass::Shutdown()
{
	// Release the header text object
	if(m_Header)
	{
		m_Header->Shutdown();
		delete m_Header;
		m_Header = 0;
	}

	// Release the start text object
	if (m_Start)
	{
		m_Start->Shutdown();
		delete m_Start;
		m_Start = 0;
	}

	// Release the high score list text object
	if (m_HighScoreList)
	{
		m_HighScoreList->Shutdown();
		delete m_HighScoreList;
		m_HighScoreList = 0;
	}

	if(m_Quit)
	{
		m_Quit->Shutdown();
		delete m_Quit;
		m_Quit = 0;
	}

	// Release the texture shader object.
	if(m_TextureShader)
	{
		m_TextureShader->Shutdown();
		delete m_TextureShader;
		m_TextureShader = 0;
	}

	// Release the camera object.
	if(m_Camera)
	{
		delete m_Camera;
		m_Camera = 0;
	}

	// Release the D3D object.
	if(m_D3D)
	{
		m_D3D->Shutdown();
		delete m_D3D;
		m_D3D = 0;
	}
}

bool MenuClass::Frame(int x, int y)
{
	return true;
}

bool MenuClass::Render()
{
	D3DXMATRIX worldMatrix, viewMatrix, projectionMatrix, orthoMatrix;
	bool result;


	// Clear the buffers to begin the scene.
	m_D3D->BeginScene(0.0f, 0.0f, 0.0f, 1.0f);

	// Generate the view matrix based on the camera's position.
	m_Camera->Render();

	// Get the world, view, projection, and ortho matrices from the camera and d3d objects.
	m_Camera->GetViewMatrix(viewMatrix);
	m_D3D->GetWorldMatrix(worldMatrix);
	m_D3D->GetProjectionMatrix(projectionMatrix);
	m_D3D->GetOrthoMatrix(orthoMatrix);

	// Turn off the Z buffer to begin all 2D rendering.
	m_D3D->TurnZBufferOff();

	result = m_Header->Render(m_D3D->GetDeviceContext(), worldMatrix, orthoMatrix);
	if(!result)
	{
		return false;
	}

	result = m_Start->Render(m_D3D->GetDeviceContext(), worldMatrix, orthoMatrix);
	if(!result)
	{
		return false;
	}

	result = m_HighScoreList->Render(m_D3D->GetDeviceContext(), worldMatrix, orthoMatrix);
	if(!result)
	{
		return false;
	}

	result = m_Quit->Render(m_D3D->GetDeviceContext(), worldMatrix, orthoMatrix);
	if(!result)
	{
		return false;
	}

	// Turn the Z buffer back on now that all 2D rendering has completed.
	m_D3D->TurnZBufferOn();

	// Present the rendered scene to the screen.
	m_D3D->EndScene();

	return true;
}
